package sis.ra.yahooNewsSummary;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import sis.ra.partofspeech.AlliasAnalysis;
import sis.ra.partofspeech.wordindex;
import sis.ra.utility.InxEntity;
import sis.ra.utility.Utils;
import sis.ra.yahoor.result.yahooresults;


public class parseYahooResults {

	public static ArrayList<yahooresults> getYahooResult(String yahooxml)
	{
		Document document = null;
		ArrayList<yahooresults> result=new ArrayList<yahooresults>();
		try {
			document = new SAXBuilder().build(new StringReader(yahooxml));
		} catch (Exception e) {
			System.out.println(yahooxml);
			e.printStackTrace();
		}
		Element root = document.getRootElement();
		System.out.println(root);
		Namespace ns = Namespace.getNamespace("urn:yahoo:yn");
		List resultsets=root.getChildren();
		for (int i=0;i<resultsets.size();i++)
		{
			System.out.println(resultsets.get(i));
			Element res=(Element)resultsets.get(i);
			String abs=res.getChild("Summary",ns).getText();
			String title=res.getChild("Title",ns).getText();
			String url=res.getChild("Url",ns).getText();
			yahooresults yr=new yahooresults(abs,title,url);
			result.add(yr);
		}		
//		}
		return result;
	}
	
	public static ArrayList <String> getPattern(ArrayList <String> sentence, String product)
	{
		ArrayList <String> pattern=new ArrayList<String>();
		String companyname="";
		for (int i=0;i<sentence.size();i++)
		{
			if (sentence.get(i).contains("COMPANY"))
			{
				pattern.add("COMPANY");
				companyname=sentence.get(i).split("\t")[3];
	//			System.out.println(companyname);
			}
			else{
				boolean isProducts=false;
				if (sentence.get(i).contains(product.trim().toLowerCase())&&!sentence.get(i).contains("COMPANY"))
				{
					pattern.add("PRODUCTS");
					isProducts=true;
				}
				if (isProducts) pattern.add("PRODUCTS");
				else 
				{
					String t=sentence.get(i).split("\t")[1];
					String t1=sentence.get(i).split("\t")[2];
					if (companyname.contains(t)) continue;
					if (t1.contains("V-Pres-3-Sg")||t1.contains("V-Pres-Sg-be"))
						pattern.add(t);
					else
					pattern.add(sentence.get(i));
				}
			}				
		}
		return pattern;
	}
	
	public static boolean isContain(String sentence, String company, String product)
	{
//		System.out.println(company+" "+product+" "+sentence);
		String [] companys=company.split(" ");
		String [] products=product.split(" ");
		
		for (int i=0;i<companys.length;i++)
			if (!sentence.contains(companys[i]))  return false;
		
		for (int i=0;i<products.length;i++)
			if (!sentence.contains(products[i]))  return false;
		
		return true;
	}
	
	public static String findProductsCompany(String sentence, String company, String product)
	{
		if (sentence.contains(product))
			sentence=sentence.replace(product, "PRODUCT");
		if (sentence.contains(company))
			sentence=sentence.replace(company, "COMPANY");
		return sentence;
	}
	
	
	public static HashMap<Integer, wordindex> pos (String text, String htformat)
	{
		String linguistxpath="c:\\Program Files\\INXIGHT\\LinguistxPlatform3.7\\lx-3\\";
		String posresult="";
		Utils.writeFile(linguistxpath+"windows-2003-i686-msvc-7.1-32\\test.txt ", text);
		String s="";
		String encoding="";
		String format="";
		String language="";
		String sentencestart="";
		String sentenceend="";
		int start=0;
		int end=0;
		int offset=0;
		Integer count=0;
		HashMap<Integer,wordindex> sentenceindex=new HashMap<Integer, wordindex> ();
		ArrayList<wordindex> sentenceindexArray=new ArrayList<wordindex> ();
		ArrayList<wordindex> posindexArray=new ArrayList<wordindex> ();
		
		String cmd="\""+linguistxpath+"windows-2003-i686-msvc-7.1-32\\test_platform.exe\" -k 735442535d524e "+
//		" -tag " +
		" -tokenize " + 
		"-d " + "\""+linguistxpath + "lang\"" + 
//		" -stem " +
		" -"+htformat+" " +"\"" +linguistxpath + "windows-2003-i686-msvc-7.1-32\\test.txt\" ";
		try {
//			System.out.println(cmd);
		     Process p = Runtime.getRuntime().exec(cmd);
		    
		    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
		    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
		    while ((s = stdInput.readLine()) != null)   
		    {
	//	    	System.out.println(s);
		    	// analyze token part
		    	if (s.contains("segment tokenization -- tokens:"))
		    	{
		    		while (!(s= stdInput.readLine()).contains("segment tokenization -- normalized tokens:"))
		    		{
		    			if (s.startsWith("  ")&&s.contains("start=")&&s.contains("end=")&&!s.startsWith("     "))
		    				{
		//    					System.out.println(s);
		    					String [] t=s.split("\t");
		    					if (t.length<4) continue;
		    					//wordindex(String pos, Integer position, String token, String type, String word)
		    					wordindex wi=new wordindex("", Integer.parseInt(t[2].substring(6).trim()),"","", t[0].trim());
		    					sentenceindex.put(Integer.parseInt(t[2].substring(6).trim()), wi);
		    					sentenceindexArray.add(wi);
		    				}
		    		}
		    	}
		    	
		    	if (s.contains("segment tokenization -- tags:"))
		    	{
		    		
		    		while ((s= stdInput.readLine())!=null&&!s.contains("The character encoding of this segment is"))
		    		{
		    			
		    			if (s.startsWith("  ")&&s.trim().length()>2&&!s.startsWith("      "))
		    			{
	//	    				System.out.println("=="+s);
		    				String [] t=s.split("\t");
		    				if (t.length>2) continue;
		    				//public wordindex(String entity, String entitytype, String pos,Integer position, String token, String type, String word) {
	//	    				System.out.println(count+" "+sentenceindexArray.get(count).getWord());
		    				sentenceindexArray.get(count).setPos(t[1].trim());
		    				
		    				count=count+1;
		    			}		    			
		    		}
		    	}
		    }
	//	    while ((s = stdError.readLine()) != null)   System.out.println(s);
		}
		catch (IOException e) {
			    e.printStackTrace();    
		}

		return sentenceindex;
	}
	
	public static ArrayList<String> getResultList (HashMap<Integer, wordindex> posresult)
	{
		ArrayList<String> sentence = new  ArrayList<String>();
		TreeSet<Integer> sortedSet=new TreeSet<Integer>(posresult.keySet());
		Object [] sortedArray=sortedSet.toArray();
		//output the wordindexresult
		String out="";
		for (int i=0;i<sortedArray.length;i++)
		{
			if (posresult.get(sortedArray[i]).getEntity()==null)
				out=sortedArray[i]+"\t"+posresult.get(sortedArray[i]).getWord()+"\t"+posresult.get(sortedArray[i]).getPos()+"\n";
			else
			{
				if (posresult.get(sortedArray[i]).getReference()==null)
				out=sortedArray[i]+"\t"+posresult.get(sortedArray[i]).getWord()+"\t"+posresult.get(sortedArray[i]).getPos()+"\t"+
					posresult.get(sortedArray[i]).getEntity()+"\t"+posresult.get(sortedArray[i]).getEntitytype()+"\n";		
				else
					out=sortedArray[i]+"\t"+posresult.get(sortedArray[i]).getWord()+"\t"+posresult.get(sortedArray[i]).getPos()+"\t"+
					posresult.get(sortedArray[i]).getEntity()+"\t"+posresult.get(sortedArray[i]).getEntitytype()+"\t"+posresult.get(sortedArray[i]).getReference()+"\n";		
			}
			sentence.add(out);
		}
		return sentence;
	}
	
	public static HashMap<Integer, wordindex> posNeAlias(String contents)
	{
		String ne=Utils.getNE(contents);
		HashMap<Integer,InxEntity> nes=Utils.parseIMSXMLContentHash(ne);
		HashMap<Integer,String> aliasindex=AlliasAnalysis.aliasAnalysis(ne);
		TreeSet<Integer> sortedSetne=new TreeSet<Integer>(nes.keySet());
		Object [] sortedArrayne=sortedSetne.toArray();
		HashMap<Integer,wordindex> posresult=pos(contents, "text");
		TreeSet<Integer> sortedSet=new TreeSet<Integer>(posresult.keySet());
		Object [] sortedArray=sortedSet.toArray();
		for (int i=0;i<sortedArrayne.length;i++)
		{
			Integer neindexnow=(Integer)sortedArrayne[i];
			Integer posindexnow=neindexnow;
//			System.out.println(nes.get(neindexnow).getSurface()+" "+nes.get(neindexnow).getEntityType());
			if (posresult.containsKey(posindexnow)&&nes.get(neindexnow).getSurface().startsWith(posresult.get(posindexnow).getWord()) ) 
			{
				
//				System.out.println(neindexnow+" "+nes.get(neindexnow).getEntityType()+" "+nes.get(neindexnow).getSurface());
				posresult.get(posindexnow).setEntitytype(nes.get(neindexnow).getEntityType());
				posresult.get(posindexnow).setEntity(nes.get(neindexnow).getSurface());	
				if (aliasindex.containsKey(neindexnow))
					posresult.get(posindexnow).setReference(aliasindex.get(neindexnow).split("\t")[1]);
				//System.out.println(neindexnow+" "+nes.get(neindexnow).getEntityType()+" "+nes.get(neindexnow).getSurface());
			}else
			{
				posindexnow=posindexnow-10;
				int loop=0;
				while (!posresult.containsKey(posindexnow)||!nes.get(neindexnow).getSurface().toLowerCase().trim().contains(posresult.get(posindexnow).getWord().toLowerCase().trim()))
				{
					posindexnow=posindexnow+1;
//					System.out.println("=="+posindexnow+" "+nes.get(neindexnow).getSurface().toLowerCase().trim());
					loop=loop+1;
					if (loop>20) break;					
				}
				if (loop>20) 
				{
					neindexnow=(Integer)sortedArrayne[i];
//					posresult.get(neindexnow).setEntitytype(nes.get(neindexnow).getEntityType());
//					posresult.get(neindexnow).setEntity(nes.get(neindexnow).getSurface());		
				}else{
				posresult.get(posindexnow).setEntitytype(nes.get(neindexnow).getEntityType());
				posresult.get(posindexnow).setEntity(nes.get(neindexnow).getSurface());	
				if (aliasindex.containsKey(neindexnow))
					posresult.get(posindexnow).setReference(aliasindex.get(neindexnow).split("\t")[1]);
				}
			}
		}
		return posresult;
	}
	
	public static void main(String[] args) {
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo company\\";
		
		File filepaths=new File (filepath);
		String[] files=filepaths.list();
		for (int i=0;i<files.length;i++)
		{
			if (!files[i].endsWith(".yah")) continue;
			String content=Utils.readFile(filepath+files[i]).trim().replace("", "").replace("", "").replace("", "").replace("", "").replace("&", "");
	//		System.out.println(content);
			ArrayList<yahooresults> rs=getYahooResult(content);
			
			for (int j=0;j<rs.size();j++)
			{
				System.out.println(rs.get(j).getAbs());
			}
		}
	}

}
